package cz.muni.fi.pv112.cranesimulator.utils.handlers.opengl;

import com.jogamp.opengl.util.texture.Texture;
import cz.muni.fi.pv112.cranesimulator.models.Model;
import cz.muni.fi.pv112.cranesimulator.models.OBJModel;
import javax.media.opengl.GL2;
import static cz.muni.fi.pv112.cranesimulator.utils.handlers.opengl.ModelDisplayHandler.drawOBJModel;
import static cz.muni.fi.pv112.cranesimulator.utils.handlers.opengl.ModelDisplayHandler.drawQuadsPolygonModel;

/**
 *
 * @author Michal Ďuriš
 */
public class DisplayListHandler {

    public static void storeModelsToDisplayList(GL2 gl, Model[] models,
                                                Texture[] textures,
                                                int textureWrapSValue,
                                                int textureWrapTValue) {
        for (Model model : models) {
            model.setDisplayListID(storeModelToDisplayList(gl, model, textures,
                    textureWrapSValue,
                    textureWrapTValue));
        }
    }

    public static int storeModelToDisplayList(GL2 gl, Model model,
                                              Texture[] textures,
                                              int textureWrapSValue,
                                              int textureWrapTValue) {
        int displayListID = gl.glGenLists(1);

        gl.glNewList(displayListID, GL2.GL_COMPILE);
        drawQuadsPolygonModel(gl, model, textures, textureWrapSValue,
                textureWrapTValue);
        gl.glEndList();

        return displayListID;
    }

    public static void storeModelsToDisplayList(GL2 gl, Model[] models,
                                                Texture texture,
                                                int textureWrapSValue,
                                                int textureWrapTValue) {
        for (Model model : models) {
            model.setDisplayListID(storeModelToDisplayList(gl, model, texture,
                    textureWrapSValue,
                    textureWrapTValue));
        }
    }

    public static int storeModelToDisplayList(GL2 gl, Model model,
                                              Texture texture,
                                              int textureWrapSValue,
                                              int textureWrapTValue) {
        int displayListID = gl.glGenLists(1);

        gl.glNewList(displayListID, GL2.GL_COMPILE);
        drawQuadsPolygonModel(gl, model, texture, textureWrapSValue,
                textureWrapTValue);
        gl.glEndList();

        return displayListID;
    }

    public static void storeOBJModelsToDiplayList(GL2 gl, OBJModel[] models,
                                                  Texture texture) {
        for (OBJModel model : models) {
            model.setDisplayListID(
                    storeOBJModelToDisplayList(gl, model, texture));
        }
    }

    public static int storeOBJModelToDisplayList(GL2 gl, OBJModel model,
                                                 Texture texture) {
        int displayListID = gl.glGenLists(1);

        gl.glNewList(displayListID, GL2.GL_COMPILE);
        drawOBJModel(gl, model, texture); //@TODO: Maybe i should add parametres for texture wrap etc...
        gl.glEndList();

        return displayListID;
    }

    public static void displayDisplayList(GL2 gl, DisplayListStorable model) {
        gl.glCallList(model.getDisplayListID());
    }

    public static void displayDisplayLists(GL2 gl, DisplayListStorable[] models) {
        for (DisplayListStorable model : models) {
            displayDisplayList(gl, model);
        }
    }

    public static void deleteDisplayList(GL2 gl, DisplayListStorable model) {
        gl.glDeleteLists(model.getDisplayListID(), 1);
    }

    public static void deleteDiplayLists(GL2 gl, DisplayListStorable[] models) {
        for (DisplayListStorable model : models) {
            deleteDisplayList(gl, model);
        }
    }

}
